#include "Bit.h"
using namespace DigitalSimulations;
/**
Binary and returns a if a & b.
*/
char Bit::and(char a, char b){
	return a & b;
}
/**
Binary or returns a char representing a | b.
*/
char Bit::or(char a, char b){
	return a | b;
}
/**
Binary xor returns a if a > b; b if b > a, and the lowest if a == b.
*/
char Bit::xor(char a, char b){
	return (((a<<7) ^ (b<<7))>>7) + '0';
}

/**
BInary not returns a with LSB flipped.
*/
char Bit::not(char a){
	return a ^ 1;
}

/**
Binary not returns not (a and b).
*/
char nand(char a, char b){
	return Bit::not(Bit::and(a,b));
}
/**
Binary nor returns not (a or b).
*/
char Bit::nor(char a, char b){
	return Bit::not(Bit::or(a,b));
}

/**
Binary addition, takes two characters a and b representing 0 or 1, and carry in as 0 or 1.
returns the sum of a and b as sum and carry out. (carry out = MSB)
*/
void Bit::add(char a, char b, char ci, char *sum, char *cu){

	char n1 = Bit::xor(a,b);
	char n2 = Bit::or(a,b);
	char n3 = Bit::and(ci, n2);
	char n4 = Bit::and(a,b);

	*sum = Bit::xor(ci,n1);
	*cu = Bit::or(n3, n4);
}


